package com.argrace.smart.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.aliyun.alink.business.alink.ALinkBusinessEx;
import com.aliyun.alink.sdk.net.anet.api.AError;
import com.aliyun.alink.sdk.net.anet.api.transitorynet.TransitoryRequest;
import com.aliyun.alink.sdk.net.anet.api.transitorynet.TransitoryResponse;
import com.argrace.smart.ArgraceApplication;
import com.argrace.smart.R;
import com.argrace.smart.activity.QingJingDetailActivity;
import com.argrace.smart.adapter.QingJingAdapter;
import com.argrace.smart.base.BaseFragment;
import com.argrace.smart.model.LocalDb_QingJingTable;
import com.argrace.smart.ui.EmptyRecycleView;
import com.argrace.smart.util.AlinkHttpUtil;
import com.argrace.smart.util.utils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by argrace_dev_1 on 2018/2/8.
 */

public class QingJingFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener {
    EmptyRecycleView recyclerView;
    SwipeRefreshLayout refreshLayout;
    private List<LocalDb_QingJingTable> lists = new ArrayList<>();
    private QingJingAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_wangguan, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        init();
        super.onActivityCreated(savedInstanceState);
    }

    private void init() {
        ArgraceApplication.is_RefreshQingJingTable = true;
        lists.addAll(DataSupport.findAll(LocalDb_QingJingTable.class));
        setView();
    }

    private void setView() {
        recyclerView = (EmptyRecycleView) getView().findViewById(R.id.recyclerView);
        refreshLayout = (SwipeRefreshLayout) getView().findViewById(R.id.refreshLayout);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter = new QingJingAdapter(lists, getActivity());
        adapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        recyclerView.setAdapter(adapter);
//        refreshLayout.setColorSchemeColors(getResources().getColor(R.color.colorTheme), getResources().getColor(R.color.colorTheme), getResources().getColor(R.color.colorTheme));//getResources().getColor(R.color.colorTheme), getResources().getColor(R.color.colorTheme), getResources().getColor(R.color.colorTheme)
        refreshLayout.setOnRefreshListener(this);
        adapter.setListeners(new QingJingAdapter.OnAdapterItemClickListener() {
            @Override
            public void click(LocalDb_QingJingTable bean, int position) {
                Intent intent = new Intent(getActivity(), QingJingDetailActivity.class);
                intent.putExtra("uuid", bean.getDeviceId());
                intent.putExtra("name", bean.getName());
                startActivity(intent);
            }
        });
        adapter.setlongListeners(new QingJingAdapter.OnAdapterItemLongClickListener() {
            @Override
            public void click(LocalDb_QingJingTable bean, int position) {
                startQingJing(bean);
            }
        });
    }

    @Override
    public void onRefresh() {
        setRefreshing(true);
        lists.clear();
        lists.addAll(DataSupport.findAll(LocalDb_QingJingTable.class));
        adapter.notifyDataSetChanged();
        setRefreshing(false);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (ArgraceApplication.is_RefreshQingJingTable) {
            onRefresh();
            ArgraceApplication.is_RefreshQingJingTable = false;
        }
    }

    public void setRefreshing(final boolean refreshing) {
        refreshLayout.post(new Runnable() {
            @Override
            public void run() {
                refreshLayout.setRefreshing(refreshing);
            }
        });
    }

    private void startQingJing(LocalDb_QingJingTable bean) {
        utils.executeQingJing(bean);
    }

    private void sendMsg(String uuid, String attrparams) {
        Log.i("info", "调用了情景中的发送，uuid是" + uuid + "参数=" + attrparams);
        String method = "mtop.openalink.app.core.device.set.status";
        HashMap<String, Object> params = new HashMap<>();
        params.put("uuid", uuid);
        params.put("setParams", attrparams);
        AlinkHttpUtil.AlinkHttpRequest(method, params, new ALinkBusinessEx.IListener() {
            @Override
            public void onSuccess(TransitoryRequest transitoryRequest, TransitoryResponse transitoryResponse) {
                Log.i("info", "发送消息成功" + transitoryResponse.data);
//                getDeviceDetailInfo();
            }

            @Override
            public void onFailed(TransitoryRequest transitoryRequest, AError aError) {
                Log.i("info", "发送消息失败");
            }
        });
    }
}
